package org.lanqiao.service.impl;




import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lanqiao.admin.IAdministratorService;
import org.lanqiao.admins.Administrator;
import org.lanqiao.mapper.AdministratorMapper;
import org.lanqiao.users.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class AdministratorServiceImpl implements IAdministratorService {
    @Autowired
    private AdministratorMapper administratorMapper;
    @Override
    public List<Administrator> administratorList() {
        return administratorMapper.selectList(null);
    }

    @Override
    public List<Administrator> administratorListByCondition(Map<String, Object> queryMap) {
        return administratorMapper.selectList(queryWrapper(queryMap)) ;
    }

    @Override
    public IPage<Administrator> administratorPage(Integer pagenum, Integer pagesize) {
            IPage<Administrator> page = new Page<>(pagenum,pagesize);
            page =administratorMapper.selectPage(page,null);
            return page;

    }

    @Override
    public IPage<Administrator> administratorPageByCondition(Integer pagenum, Integer pagesize, Map<String, Object> queryMap) {
        IPage<Administrator> page = new Page<>(pagenum,pagesize);
        page = administratorMapper.selectPage(page,queryWrapper(queryMap));
        return page;
    }

    @Override
    public Integer addAdministrator(Administrator administrator) {
        if(administrator == null){
            return 0;
        }else {
            try {
                administratorMapper.insert(administrator);
                return  1;
            }catch (Exception e){
                e.printStackTrace();
                return -1;
            }
        }
    }

    @Override
    public Integer updateAdministrator(Administrator administrator) {
        if(administrator == null){
            return 0;
        }else {
            try {
                administratorMapper.updateById(administrator);
                return  1;
            }catch (Exception e){
                e.printStackTrace();
                return -1;
            }
        }
    }

    @Override
    public Integer deleteAdministratorById(Integer id) {
         try {
                administratorMapper.deleteById(id);
                return  1;
            }catch (Exception e){
                e.printStackTrace();
                return -1;
            }

    }

    @Override
    public Administrator queryAdminByName(String loginname) {
        return administratorMapper.selectOne(new QueryWrapper<Administrator>().eq("login_name",loginname));
    }


    private QueryWrapper<Administrator> queryWrapper(Map<String,Object> queryMap){
        // 查询条件
        QueryWrapper<Administrator> queryWrapper = null;
        if(queryMap !=null){
            queryWrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(queryMap.get("name"))){
                queryWrapper.like("login_name",queryMap.get("name"));
            }
            if(!StringUtils.isEmpty(queryMap.get("status"))){
                queryWrapper.eq("status",queryMap.get("status"));
            }
            if(!StringUtils.isEmpty(queryMap.get("role"))){
                queryWrapper.eq("role",queryMap.get("role"));
            }
        }
        return  queryWrapper;
    }
}
